package com.m3958.firstgwt.server.model;

import java.util.Date;

import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.QueryHint;
import javax.persistence.Table;

import org.eclipse.persistence.config.CacheUsage;
import org.eclipse.persistence.config.HintValues;
import org.eclipse.persistence.config.QueryHints;

import com.m3958.firstgwt.server.types.JsonExcludeFields;
import com.m3958.firstgwt.server.utils.JsonlibDateValueProssor;


import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

@Entity
@Table(name="WF_CHANGE_LOG")
@AttributeOverride(name="id",column=@Column(nullable = false,name="WF_LOG_ID"))
@NamedQueries({
	@NamedQuery(name="findUserLogAfterMe",
			query="SELECT w FROM WFChangeLog AS w WHERE w.id > ?1 ORDER BY w.id ASC",
			hints={@QueryHint(name=QueryHints.CACHE_USAGE, value=CacheUsage.DoNotCheckCache),
			@QueryHint(name=QueryHints.REFRESH, value=HintValues.TRUE)}),
	@NamedQuery(name="findUserLastChangeLog",
			query="SELECT w FROM WFChangeLog AS w WHERE w.creator.id = ?1 ORDER BY w.id DESC",
			hints={@QueryHint(name=QueryHints.CACHE_USAGE, value=CacheUsage.DoNotCheckCache),
			@QueryHint(name=QueryHints.REFRESH, value=HintValues.TRUE)}),
	@NamedQuery(name="findItemLogAfterMe",
			query="SELECT w FROM WFChangeLog AS w WHERE w.id > ?1 AND w.changeDelta LIKE ?2 ORDER BY w.id ASC",
			hints={@QueryHint(name=QueryHints.CACHE_USAGE, value=CacheUsage.DoNotCheckCache),
			@QueryHint(name=QueryHints.REFRESH, value=HintValues.TRUE)}),
	@NamedQuery(name="findItemLastChangeLog",
			query="SELECT w FROM WFChangeLog AS w WHERE w.changeDelta LIKE ?1 ORDER BY w.id DESC",
			hints={@QueryHint(name=QueryHints.CACHE_USAGE, value=CacheUsage.DoNotCheckCache),
			@QueryHint(name=QueryHints.REFRESH, value=HintValues.TRUE)})
})
public class WFChangeLog extends BaseModel<WFChangeLog> implements HasCreator{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public static class NamedQueries{
		public final static String FIND_USER_LOG_AFTER_ME = "findUserLogAfterMe";
		public final static String FIND_USER_LAST_LOG = "findUserLastChangeLog";
		public final static String FIND_ITEM_LOG_AFTER_ME = "findItemLogAfterMe";
		public final static String FIND_ITEM_LAST_LOG = "findItemLastChangeLog";
	}

	@Override
	public JSONObject toJson(){
		JsonConfig jc = new JsonConfig();
		jc.registerJsonValueProcessor(Date.class, injector.getInstance(JsonlibDateValueProssor.class));
		jc.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		jc.setExcludes(JsonExcludeFields.WF_LOG_EXCLUDES);
		return JSONObject.fromObject(this, jc);
	}

	@Lob
	private String changeDelta;
	
	@Column(length=36)
	private String mid;//被shared的项目的根目录
	
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="CREATOR_ID",referencedColumnName="USER_ID")
	protected User creator;
	
	
	public User getCreator() {
		return creator;
	}

	public void setCreator(User creator) {
		this.creator = creator;
	}

	@Override
	public String[] getJsonExcludes() {
		return null;
	}

	@Override
	public String getCreatorIds() {
		return "";
	}

	public void setChangeDelta(String changeDelta) {
		this.changeDelta = changeDelta;
	}

	public String getChangeDelta() {
		return changeDelta;
	}

	public void setMid(String mid) {
		this.mid = mid;
	}

	public String getMid() {
		return mid;
	}

}
